package leetcode.day;




public class TitleToNumber {
    private static int x = 100;
    public static void main(String[] args) {
        System.out.println(64 - (int) 'Z');
        System.out.println((char) (26 + 64));
        System.out.println(convertToTitle2(2147483647));
        TitleToNumber titleToNumber = new TitleToNumber();
        System.out.println(titleToNumber.x);

    }

    // 171. Excel 表列序号
    // 26个字母相当于26进制，每26个数往前走一位
    public static int titleToNumber(String columnTitle) {
        int res = 0, num = 0;
        for (int i = 0; i < columnTitle.length(); i++) {
            num = Math.abs(64 - (int) columnTitle.charAt(i));
            res = res * 26 + num;
        }
        return res;
    }

    // 168. Excel表列名称，与上一题互逆
    public static String convertToTitle(int columnNumber) {
        StringBuilder sb = new StringBuilder();
        while (columnNumber > 0) {
            columnNumber--;
            sb.append((char) (columnNumber % 26 + 'A'));
            columnNumber /= 26;
        }
        return sb.reverse().toString();
    }

    // 想法错误
    public static String convertToTitle2(int columnNumber) {
        int m = columnNumber / 26;
        int n = columnNumber % 26;
        String res="";
        if (m!=0){
            char M = (char) (m + 64);
            char N = (char) (n + 64);
          return  res = M+String.valueOf(N);
        }else {
            char N = (char) (n + 64);
            return  res = String.valueOf(N);
        }

    }
}
